package kz.asoft;


import java.awt.Color;

import java.util.Calendar;

import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import kz.asoft.am.common.RisksAppModule;
import kz.asoft.utils.ADFUtils;

import oracle.adf.view.faces.bi.component.pivotTable.CellFormat;
import oracle.adf.view.faces.bi.component.pivotTable.DataCellContext;
import oracle.adf.view.faces.bi.component.pivotTable.HeaderCellContext;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;

import oracle.bali.ewt.graphics.ColorUtils;

import oracle.dss.util.QDR;


public class CtrlObjManageBean {
    private RichInputText searchText;
    private RichSelectOneChoice year;

    public CtrlObjManageBean() {
    }

    /**
     * Custom header format for ctrl obj risk report
     * @param headerCellContext
     * @return
     */
    public CellFormat headerFormat(HeaderCellContext headerCellContext) {
        CellFormat cellFormat = new CellFormat(null, null, null);
        if (headerCellContext.getValue() != null) {
            QDR qdr = headerCellContext.getQDR();
            Object name1 = qdr.getDimMember("Name1");
            Object code = qdr.getDimMember("Code");
            Object name = qdr.getDimMember("ShortName");
            
            if (name1 != null) {
                String style = "width:100px;white-space:normal;";
                cellFormat.setTextStyle(style);
            } else if (code != null) {
                String color = null;
                if (code.toString().equals("1")) {
                    color =
                            "background-color:" + ColorUtils.colorToHTML(Color.GREEN) +
                            ";" + "white-space:normal;";
                } else if (code.toString().equals("2") ||
                           code.toString().equals("3")) {
                    color =
                            "background-color:" + ColorUtils.colorToHTML(Color.YELLOW) +
                            ";";
                } else if (code.toString().equals("4") ||
                           code.toString().equals("5")) {
                    color =
                            "background-color:" + ColorUtils.colorToHTML(Color.RED) +
                            ";";
                }
                cellFormat.setStyle(color);
            } else if (name != null){
                String style = "width:150px;white-space:normal;";
                cellFormat.setTextStyle(style);    
            }
        }
        return cellFormat;
    }

    /**
     * Поиск ОК по наименованию
     * @param actionEvent
     */
    public void searchAction(ActionEvent actionEvent) {
        RisksAppModule appModule =
            (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        int yearInt;
        if (year != null && year.getValue() != null) {
            yearInt = Integer.valueOf(year.getValue().toString());
        } else {
            yearInt = Calendar.getInstance().get(Calendar.YEAR);
        }
        appModule.searchCtrlObj(searchText.getValue() == null ? "" :
                                searchText.getValue().toString(), yearInt);
    }

    public void setSearchText(RichInputText searchText) {
        this.searchText = searchText;
    }

    public RichInputText getSearchText() {
        return searchText;
    }

    /**
     * Список ОК без фильтрации
     * @param valueChangeEvent
     */
    public void valueChangeListener(ValueChangeEvent valueChangeEvent) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIViewRoot root = facesContext.getViewRoot();
        RichCommandButton button =
            (RichCommandButton)root.findComponent("reload");
        if (button != null) {
            ActionEvent actionEvent = new ActionEvent(button);
            actionEvent.queue();
        }
        RisksAppModule appModule = (RisksAppModule)ADFUtils.getApplicationModuleForDataControl("RisksAppModuleDataControl");
        appModule.searchCtrlObj("", Calendar.getInstance().get(Calendar.YEAR));
    }

    public void setYear(RichSelectOneChoice year) {
        this.year = year;
    }

    public RichSelectOneChoice getYear() {
        return year;
    }
}
